package cn.tangsihai.order.controller;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.util.AjaxResult;
import cn.tangsihai.order.dto.BatchOrderDto;
import cn.tangsihai.order.dto.OrderDto;
import cn.tangsihai.order.service.IOrderPetService;
import jdk.internal.instrumentation.TypeMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderPetService orderPetService;

    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderDto orderDto){
        try {
            String orderSn = orderPetService.submitOrder(orderDto);
            return AjaxResult.me().setResultObj(orderSn);
        }catch (MyException e) {
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody BatchOrderDto batchOrderDto){
        try {
            List list = orderPetService.batchOrder(batchOrderDto);
            return AjaxResult.me().setResultObj(list);
        }catch (MyException e) {
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }
}
